package com.demoworld;

import com.demoworld.dao.CustomMapperFactory;
import com.demoworld.dao.DBHelper;
import com.demoworld.dao.IBankDao;
import com.demoworld.dao.IUserDao;
import com.demoworld.entity.User;
import com.demoworld.entity.UserBankAccountInfo;
import org.junit.Test;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.*;
import java.util.UUID;

/**
 * Created by francis on 2017/6/14.
 */
public class main {
    static String sql = null;
    static DBHelper db1 = null;
    static ResultSet ret = null;
    //更新数据
    @Test
    public void updateDemo() throws IOException, SQLException {
        sql = "update tb_user set name='ss' where id='30fdacf4-db1a-44ea-8421-d105595d1393'";
        String sql1 = "update tb_user set name='ss1' where id='4acd4b98-b4fb-4725-a2c1-d5a20106980d'";
        db1 = new DBHelper();
        db1.excuteUpdate();
        db1.conn.setAutoCommit(false);  //将自动提交设置为false
        boolean flag1 = false;
        boolean flag2 = false;
        try {
            flag1 = db1.statement.executeUpdate(sql) > 0;//假设A扣款
            //--------------------如果涉及flag2执行前，加断点，运行selectDemo(), -----------------------

            flag2 = db1.statement.executeUpdate(sql1) > 0;//假设B加钱
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (flag1 && flag2) {
                db1.conn.commit(); // 提交会话，即事务提交
            } else {
                db1.conn.rollback();
            }
            db1.statement.close();
            db1.conn.close();//关闭连接, 一定要关闭
        }
    }
    //查看全部数据
    @Test
    public void selectDemo() throws IOException, SQLException {
        sql = "select * from tb_user";
        db1 = new DBHelper();
        db1.excuteSelect(sql);
        try {
            ret = db1.pst.executeQuery();//执行语句，得到结果集
            while (ret.next()) {
                String uid = ret.getString(1);
                String ufname = ret.getString(2);
                String ulname = ret.getString(3);
                System.out.println(uid + "\t" + ufname + "\t" + ulname);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ret.close();
            db1.close();//关闭连接, 一定要关闭
        }
    }
}
